package com.hmkj.crm.billing.service.company.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmkj.crm.billing.domain.company.InventoryDetail;
import com.hmkj.crm.billing.enums.AmountTypeEnum;
import com.hmkj.crm.billing.mapper.company.InventoryDetailMapper;
import com.hmkj.crm.billing.service.company.DeliveryCompaniesInfoService;
import com.hmkj.crm.billing.service.company.InventoryDetailService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 账单 服务
 *
 * @author sks.lwei
 * @date 2020/02/20
 */
@Log4j2
@Service
public class InventoryDetailServiceImpl extends ServiceImpl<InventoryDetailMapper, InventoryDetail> implements InventoryDetailService {

    /**
     * 配送公司
     */
    @Resource
    private DeliveryCompaniesInfoService deliveryCompaniesInfoService;

    @Override
    public IPage<InventoryDetail> listInventoryDetailPage(Page<InventoryDetail> page, Wrapper<InventoryDetail> wrapper) {
        return this.page(page, wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void addDetail(InventoryDetail inventoryDetail) {
        log.info("添加公司账户账单操作");

        inventoryDetail.setUuid("inv_"+ IdWorker.get32UUID());
        //获取公司账户余额
        Double deliveryBalance = deliveryCompaniesInfoService.getDeliveryBalance(inventoryDetail.getCompanyUuid());
        //操作金额
        Double amount = inventoryDetail.getAmount();

        //判定记录方式
        AmountTypeEnum amountType = inventoryDetail.getAmountType();
        switch (amountType){
            //收入
            case INCOME:
            //回款
            case RECEIVABLE:
                deliveryBalance = deliveryBalance + amount;
                break;
            //支出
            case SPENDING:
            //手续费
            case POUNDAGE:
            //配送费
            case SHIPPING_FEE:
                deliveryBalance = deliveryBalance - amount;
                inventoryDetail.setAmount(-amount);
                break;
            default:
                log.warn("未知的金额类型，操作已终止");
                throw new NullPointerException("未知的金额类型");
        }
        inventoryDetail.setBalance(deliveryBalance);
        inventoryDetail.setBillTime(new Date());
        this.save(inventoryDetail);
        log.info("公司账户账单记录添加完成！");

        deliveryCompaniesInfoService.setDeliveryBalance(inventoryDetail.getCompanyUuid(), deliveryBalance);
        log.info("公司账户更新成功");

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void accountChange(String orderUuid, String companyUuid, Double amount, AmountTypeEnum amountType) {
        log.info("公司账户记录！类型：{}[{}]，公司：{}，订单号：{}，变动金额：{}", amountType.getTitle(), amountType.getType() > 0 ? "收入" : "支出", companyUuid, orderUuid, amount);

        String companiesName = deliveryCompaniesInfoService.getDeliveryCompaniesNameByUuid(companyUuid);
        if(StrUtil.isBlank(companiesName)){
            throw new NullPointerException("选定公司不存在！");
        }

        InventoryDetail inventoryDetail = new InventoryDetail();
        inventoryDetail.setCompanyUuid(companyUuid);
        inventoryDetail.setCompanyName(companiesName);
        //采购支出
        inventoryDetail.setAmountType(amountType);
        inventoryDetail.setOrderUuid(orderUuid);
        inventoryDetail.setAmount(amount);
        inventoryDetail.setRemark("系统自动记录："+amountType.getTitle());
        this.addDetail(inventoryDetail);
        log.info("系统自动记录账户变更成功");
    }


}
